package com.shenghua.config;

import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.util.DruidPasswordCallback;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
 * 生成加密密码
 * 用于druid数据库连接池
 * Created by Administrator on 2019/6/5.
 */
public class DBPasswordCallback extends DruidPasswordCallback {

    public  static final String PUBLIC_KEY_STRING = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIw7BiCAKXuEWjiWB5QdhSCoX3wDSoi+BxENEQdG6jUSCithtawU4o7bEfQmHPkzgeZL2Dl584g0Nrlg+EZVzDsCAwEAAQ==\n" +
            "password:JHIYEdPSl716i1tEFbXochn6q/5KI7zTrYiMTwi/8cKA0rkL6bgBRUc7DbgvtTivrs4ifsXJyyKNTBVZ8nd3qA==";

    @Override
    public void setProperties(Properties properties) {
        super.setProperties(properties);
        String pwd = (String) properties.getProperty("password");
        String publickey = (String) properties.get("publickey");
        if (StringUtils.isNoneBlank(pwd)) {
            //TODO
            String password = "";
            try {
                password = ConfigTools.decrypt(publickey, pwd);
//                System.err.println("-------------------------------");
//                System.err.println(password);
                setPassword(password.toCharArray());
            } catch (Exception e) {
                setPassword(password.toCharArray());
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        String originPassword = "123456";
        String newPassword = "";
        String privateKey = "MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEAjDsGIIApe4RaOJYHlB2FIKhffANKiL4HEQ0RB0bqNRIKK2G1rBTijtsR9CYc+TOB5kvYOXnziDQ2uWD4RlXMOwIDAQABAkEAi3bsWgh8uS5heGOl9+YAEAHErRas2yB68LdZFy6tIcdtAolOmf+AtmxBzJ6YTM7gox9LrGuT6Cp71iKwfK28sQIhAMXHMgS21jvAJ3D1ny1A0LC3y1wWNxMNyz4A8CbTtagJAiEAtYL5TvtPAOZpubYekUE/f3uSBs3rnXY1uUBPxLyH+yMCIDzCs6UrHqoAoJ1deGeIsBR6gqlp0cWyySvlUeI7zGUxAiEAkNHkGlk2apBsMGkXMGuvvwYXt2gaojKLoLeoLV8C26MCIQCq7nl1hDNaugq1l7oyjt6ee+j/B1OrQ9rYgy9K90tJUA==";
        String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIw7BiCAKXuEWjiWB5QdhSCoX3wDSoi+BxENEQdG6jUSCithtawU4o7bEfQmHPkzgeZL2Dl584g0Nrlg+EZVzDsCAwEAAQ==";
        newPassword = ConfigTools.encrypt(privateKey, originPassword);
        System.out.println(newPassword);
        System.out.println(ConfigTools.decrypt(publicKey, newPassword));
        //System.out.println(ConfigTools.encrypt("MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIw7BiCAKXuEWjiWB5QdhSCoX3wDSoi+BxENEQdG6jUSCithtawU4o7bEfQmHPkzgeZL2Dl584g0Nrlg+EZVzDsCAwEAAQ==",password));
        //IzJpixnGIJ6Esnzw1/DVjjFCcfUCYq/szPBZjfUOXDWVrwxp+2/71U99xyw8ZSCAuG/Jn+rpcEMd1Vs9WV4c3A==
    }
}
